package diamondServer.businesslogic.manager;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Set;

import diamondServer.businesslogic.resource.DefaultResRepository;
import diamondServer.businesslogic.resource.Resource;
import diamondServer.businesslogic.resource.ResourceData;
import diamondServer.businesslogic.resource.ResourcesRepository;
import diamondServer.resources.MessageFake;
import diamondServer.resources.ServiceFake;


import junit.framework.TestCase;

@Deprecated
public class DefaultBLManagerTest
		extends TestCase
{

	private DefaultBLManager mgr;
	private ResourcesRepository resRepository;
	
	private final ByteArrayOutputStream out =
		new ByteArrayOutputStream(); //for syso testing
	private final String LINE_SEPARATOR =
		System.getProperty("line.separator");
	
	protected void setUp() throws Exception
	{
		mgr = (DefaultBLManager) DefaultBLManager.getInstance();
		resRepository = DefaultResRepository.getInstance();
		System.setOut(new PrintStream(out));
	}

	protected void tearDown() throws Exception
	{
		resRepository.removeAll();
		System.setOut(null); 
	}

	public void testProcess()
	{
		resRepository.addResource("ResourceFake",
				new Resource(new MessageFake(),new ServiceFake()));
		mgr.process("irrelevant string");
		
		assertEquals(new MessageFake().getBody()+LINE_SEPARATOR, 
				out.toString());
	}

	public void testUpdateResources()
	{
		/* update manager by faked resource */
		ResourceData resData = new ResourceData(
				ServiceFake.class.getName(), MessageFake.class.getName());
		Set<ResourceData> resDataSet = new HashSet<ResourceData>();
		resDataSet.add(resData);
		mgr.updateResources(resDataSet);
		
		/* check repository update */
		ResourcesRepository catalog = DefaultResRepository.getInstance();
		Resource res = catalog.getResource(new MessageFake().getResourceName());
		assertEquals("The repository doesn't contain the proper Message in " +
				"the updated Resource",
				res.getMessage().getClass().getName(),
				MessageFake.class.getName());
		assertEquals("The repository doesn't contain the proper Service in " +
				"the updated Resource",
				res.getService().getClass().getName(),
				ServiceFake.class.getName());
	}
}
